function PhotoFader(nm, mainDiv, imgArr, speed, delay){
	nm = nm + Math.floor( Math.random() * 100 + 1 ).toString();
	this.name	= nm;
	this.imgArr = imgArr;
	this.curImg = 0;
	this.curDiv = 1;
	this.speed = speed;
	this.delay = delay;
	
	var mainDv = document.getElementById(mainDiv);
	
	if( !document.PFArray ) document.PFArray = new Array();
	document.PFArray[nm] = this;
			
	document.write("<style type='text/css'>\n");
	document.write("#" + nm + "_photo1 img { visibility:hidden; }\n");
	document.write("#" + nm + "_photo1 { position:absolute; z-index: 1; }\n");
	document.write("#" + nm + "_photo2 { position:absolute; z-index: 0; }\n");
	document.write("</style>");
	
	this.initImages = function() {
		document.write("<scr");
		document.write("ipt type='text/javascript'>\n");
		for(var i=0; i<this.imgArr.length; i++){
			document.write("var " + nm + "_img"+i+" = new Image();\n");
			document.write(nm + "_img"+i+".src = '"+ this.imgArr[i] +"';\n");
		}
		document.write("document.PFArray['" + nm + "'].start();\n");
		document.write("</scr");
		document.write("ipt>\n");
		
	}
	
	this.start = function(){
		var hldr1 = nm + "_photo1";
		var hldr2 = nm + "_photo2";
		
		var dv1 = document.createElement("div");
				dv1.id = nm + "_photo1";
				dv1.innerHTML = "<img src='"+ imgArr[0] +"' />";
		var dv2 = document.createElement("div");
				dv2.id = nm + "_photo2";
		
		mainDv.appendChild(dv1);
		mainDv.appendChild(dv2);
		
		var image1 = document.getElementById(hldr1).childNodes[0];
		
		PhotoFaderSetOpacity(image1, 0);
		image1.style.visibility = 'visible';
		
		document.PFArray[nm].startTime = new Date();
		PhotoFaderFadeIn(hldr1,nm);
		}
	
	this.initImages();
}
	
function PhotoFaderSetOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function PhotoFaderFadeIn(objId,nm) {
	if (document.getElementById) {
    	var obj = document.getElementById(objId).childNodes[0];
		
		var startTime = document.PFArray[nm].startTime;
		var currTime = new Date();
		var diffTime = currTime.getTime() - startTime.getTime();
		var speed = document.PFArray[nm].speed;
		
		var opacity = Math.ceil((diffTime/speed) * 100);
		if( opacity > 100 ) opacity = 100;
		
    	if (opacity < 100) {
		
			PhotoFaderSetOpacity(obj, opacity);
			window.setTimeout("PhotoFaderFadeIn('"+objId+"','"+nm+"')", 10);
		}
		else {
			PhotoFaderSetOpacity(obj, opacity);
			setTimeout("PhotoFaderSwapImages('"+nm+"')",document.PFArray[nm].delay);
		}
	}
}

function PhotoFaderSwapImages(nm){
	// find out which
	
	var pfObj = document.PFArray[nm]
	
	if(pfObj.curImg == pfObj.imgArr.length-1)
		pfObj.curImg = 0;
	else 
		++pfObj.curImg;

	// now get the div to hld the new image
	var dvName = (pfObj.curDiv == 1)? nm + "_photo2" : nm + "_photo1";
	var eDivName = (pfObj.curDiv == 1)? nm + "_photo1" : nm + "_photo2";
	pfObj.curDiv = (pfObj.curDiv == 1)?2:1;
	
	var tgtDiv = document.getElementById(dvName);
	var eDiv = document.getElementById(eDivName);
	
	// now fill the target div
	tgtDiv.innerHTML = "<img src='"+ pfObj.imgArr[pfObj.curImg] +"' style='visibility:hidden;' />";
	
	//move the divs around in z-index
	eDiv.style.zIndex = 0;
	tgtDiv.style.zIndex = 1;
	
	// And finally fade in the image
	
  var img = tgtDiv.childNodes[0];
	
  PhotoFaderSetOpacity(img, 0);
  img.style.visibility = 'visible';
  
  document.PFArray[nm].startTime = new Date();
  PhotoFaderFadeIn(tgtDiv.id,nm);
}